package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.BankBran;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 银行卡
 */
public class BankActivity extends BaseActivity implements View.OnClickListener {
    private List<BankBran> list = new ArrayList<>();
    private SwipeMenuListView listView;
    private String from;
    private BaseAdapter baseAdapter;

    @Override
    protected void onResume() {
        upload();
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank);
        initData();
        initTitle();
        listView = (SwipeMenuListView) findViewById(R.id.list);
        SwipeMenuCreator creater = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {


                //同理create删除item
                SwipeMenuItem deleteItem = new SwipeMenuItem(BankActivity.this);
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set item title
                deleteItem.setTitle("删除");
                // set item title fontsize
                deleteItem.setTitleSize(18);
                // set item title font color
                deleteItem.setTitleColor(Color.WHITE);
                // set a icon
                // deleteItem.setIcon(R.mipmap.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        if (!"提现".equals(from))
            listView.setMenuCreator(creater);
        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
                switch (index) {

                    case 0:
                        //删除的逻辑
                        showDialogv7("确定删除该管理员吗？", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                updelete(position);
                                baseAdapter.notifyDataSetChanged();
                            }
                        });
                        break;

                    default:
                        break;
                }
                return false;
            }
        });
        baseAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int position) {
                return list.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                MyHolder holder;
                if (convertView == null) {
                    convertView = View.inflate(BankActivity.this, R.layout.item_bank, null);
                    holder = new MyHolder();
                    holder.bank_img = (ImageView) convertView.findViewById(R.id.bank_img);
                    holder.bank_explain = (TextView) convertView.findViewById(R.id.bank_explain);
                    holder.bank_name_tv = (TextView) convertView.findViewById(R.id.bank_name_tv);
                    holder.ll_max = (LinearLayout) convertView.findViewById(R.id.ll_max);
                    convertView.setTag(holder);
                } else {
                    holder = (MyHolder) convertView.getTag();
                }
                x.image().bind(holder.bank_img, list.get(position).getBank_img());
                holder.bank_name_tv.setText(list.get(position).getBank_name());
                holder.bank_explain.setText(list.get(position).getDescribe());
                if ("提现".equals(from))
                    holder.ll_max.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("bankBean", list.get(position));
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    });
                return convertView;
            }

            class MyHolder {
                ImageView bank_img;
                TextView bank_name_tv, bank_explain;
                LinearLayout ll_max;
            }

        };
        listView.setAdapter(baseAdapter);
//        adapter = new BaseRVAdapter<BankBran>(this, list) {
//            @Override
//            public int getLayoutId(int viewType) {
//                return R.layout.item_bank;
//            }
//
//            @Override
//            public void onBind(BaseViewHolder holder, final int position) {
//                ImageView bank_img = holder.getView(R.id.bank_img);
//                x.image().bind(bank_img, list.get(position).getBank_img());
//                holder.setTextView(R.id.bank_name_tv, list.get(position).getBank_name());
//                holder.setTextView(R.id.bank_explain, list.get(position).getBank_branch());
//                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                    @Override
//                    public boolean onLongClick(View v) {
//                        longClickLisrener.longListener(position);
//                        return false;
//                    }
//                });
//                if ("提现".equals(from)) {
//                    holder.itemView.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            clickListener.clickListener(position);
//                        }
//                    });
//                }
//            }
//        };


    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    private void initData() {
        from = getIntent().getStringExtra("from");
    }

    private static final String TAG = "BankActivity";

    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.MONEY_GETCARDLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: BankActivity= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    if (jsonArray != null) {
                        list.clear();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            BankBran bean = new BankBran();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), BankBran.class);
                            list.add(bean);
                        }
                    }
                    baseAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("银行卡");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        TextView right_tv = (TextView) findViewById(R.id.tv_right);
        right_tv.setVisibility(View.VISIBLE);
        right_tv.setText("新增");
        right_tv.setTextColor(getResources().getColor(R.color.main_color));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            case R.id.btn_main_right:
                startActivity(new Intent(this, BankAddActivity.class));
                break;
        }
    }


    private void updelete(final int i) {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("cardId", list.get(i).getBank_id());
        XUtil.Post(Url.MONEY_DELBANKCARD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: Bank_id= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        upload();
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private inLongClickLisrener longClickLisrener = new inLongClickLisrener() {
        @Override
        public void longListener(final int index) {
            showDialogv7("确定删除该银行卡吗？", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    updelete(index);
                }
            });

        }
    };


    private interface inLongClickLisrener {
        void longListener(int index);
    }

    private onClickListener clickListener = new onClickListener() {
        @Override
        public void clickListener(int index) {
            Intent intent = new Intent();
            intent.putExtra("bankBean", list.get(index));
            setResult(RESULT_OK, intent);
            finish();
        }
    };


    private interface onClickListener {
        void clickListener(int index);
    }
}
